﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TerhelesEloszto.Common;
using TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class ModifyFact : System.Web.UI.Page
	{
		private static IEngine engine = Engine.Instance();
		IFact currentFact = null;
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}

			foreach( IFact actFact in engine.GetFacts() )
			{
				if( actFact.Neptun.Trim().Equals( Request.QueryString["ID"] ) )
				{
					currentFact = actFact;
					break;
				}
			}
			if( currentFact != null && !Page.IsPostBack )
			{

				Name.Text = currentFact.Name.Trim();
				Description.Text = currentFact.Description.Trim();
			}
			else if( currentFact == null )
			{
				Response.Redirect( "~/Admin/Fact.aspx?msg=0" );
			}
		}

		protected void submit_Click( object sender, EventArgs e )
		{
			try
			{
				engine.UpdateFact( currentFact, Name.Text, Description.Text );
				Response.Redirect( "~/Admin/Fact.aspx?msg=1" );
			}
			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Fact.aspx?msg=0" );
			}
		}
	}
}